<?php

require_once("lib/redbeanwrapper.php");

class Author extends RedBeanModel
{
	/**
	 * Returns an Author object based on a name and email adress.
	 * Returns false if the author is not found.
	 */
	public function find($name, $email)
	{
		$r = Finder::where("author", " name = :name AND email = :email",
			array(":name" => $name, ":email" => "$email"));

		if(count($r) > 0)
			return $r[0];
		else
			return false;
	}

	
	/**
	 * Adds a new author based on a name and email adress. 
	 */
	static function addAuthor($name, $email)
	{
		$auth = new Author();
		$auth->name = $name;
		$auth->email = $email;
		$auth->save();

		return $auth;
	}
	
	/**
	 * Gets an array of SimplePie_Author, checks is they are in the system 
	 * and if they are not, they are added.
	 */
	static function addAuthors($authorlist)
	{
		$authorbeans = array();

		foreach($authorlist as $author)
		{
			$foundAuthor = self::find($author->get_name(), $author->get_email());
			if(!$foundAuthor)
			{
				$auth = self::addAuthor($author->get_name(), $author->get_email());
				$authorbeans[] = $auth;
			}
			else
			{
				$authorbeans[] = $foundAuthor;
			}
		}

		return $authorbeans;
	}
}

?>
